<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2018/10/23
 * Time: 13:48
 */

namespace app\admin\service;


use app\admin\model\Comment;
use app\admin\model\Vote;
use app\admin\model\VoteItems;

class CommentService extends BaseService
{
    public $commentModel;
    public function __construct()
    {
        $this->commentModel = new Comment();
    }

    public function getPagination($status,$auto,$keyword)
    {
        $admin = session('admin');
        $where = ['b.site_id' => $admin['site_id']];
        if($keyword)
            $where['a.content|b.name|c.name|c.mid'] = ['like','%'.$keyword.'%'];
        if($status != '' && $status != 9)
            $where['a.enable'] = ['eq',$status];
        if($auto)
            $where['a.auto'] = ['eq',$auto];

        $data['list'] = $this->commentModel
            ->alias('a')
            ->join(Vote::TABLE.' b','b.id=a.vote_id')
            ->join(VoteItems::TABLE.' c','a.vote_item_id=c.id')
            ->where($where)
            ->order('a.id desc')
            ->field('a.*,b.name as vote_name,c.name,c.mid')
            ->paginate();
        $data['page'] = $data['list']->render();
        return $data;
    }

    public function update($id,$data)
    {
        $comment = $this->commentModel->find($id);
        if ($data['field'] = "enable" && $comment['auto'] == 2) {
            return false;
        }
        $res = $comment->update($data);
        if($res){
            //清除评论缓存
            if($comment)
                $this->clearComment($comment['vote_item_id']);
        }
        return $res;
    }
}